<template>
  <el-form ref="form" :label-position="device === 'mobile' ? 'top' : 'right'" label-width="100px" :model="form" :rules="rules">
    <el-form-item label="姓名" prop="name">
      <el-input v-model.trim="form.name" autocomplete="off" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="年/月" prop="yearMonth">
      <el-date-picker v-model="form.yearMonth" placeholder="请选择年/月" style="width: 100%" type="month" value-format="yyyy-MM" @input="$forceUpdate()"/>
    </el-form-item>
    <el-form-item label="薪资" prop="pay">
      <el-input v-model.trim="form.pay" autocomplete="off" placeholder="请输入薪资" type="number" />
    </el-form-item>
    <el-form-item label="其他" prop="other">
      <el-input v-model.trim="form.other" autocomplete="off" placeholder="请输入其他" :rows="2" type="textarea" />
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model.trim="form.remark" autocomplete="off" placeholder="请输入备注" :rows="2" type="textarea" />
    </el-form-item>
    <el-form-item v-if="device === 'mobile'" class="submit-content">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from "vuex";
import { API_salaryCreate, API_salaryEdit } from "@/api/salary";
export default {
  name: "FormEdit",
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        name: "",
        yearMonth: "",
        year: "",
        month: "",
        pay: "",
        other: "",
        remark: "",
      },
      rules: {
        name: [{ required: true, trigger: "blur", message: "请输入姓名" }],
        yearMonth: [
          { required: true, trigger: "blur", message: "请选择年/月" },
        ],
        pay: [{ required: true, trigger: "blur", message: "请输入薪资" }],
      },
    };
  },
  computed: {
    ...mapGetters({
      device: "settings/device",
    }),
  },
  watch: {
    formData: {
      handler(newData) {
        if (newData) {
          this.form = Object.assign({}, this.formData);
          if (this.form.code) {
            this.form.yearMonth = `${this.form.year}-${this.form.month}`;
          }
        }
      },
      immediate: true,
    },
  },
  methods: {
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      if (this.device === "mobile") {
        this.$router.push({
          name: "Salary",
          params: { refresh: true },
        });
      }
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          let params = Object.assign({}, this.form);
          params.year = params.yearMonth.split("-")[0]; // "2024"
          params.month = params.yearMonth.split("-")[1]; // "01"
          delete params.yearMonth;
          if (params.code) {
            const { message } = await API_salaryEdit(params);
            this.$baseMessage(message, "success");
          } else {
            const { message } = await API_salaryCreate(params);
            this.$baseMessage(message, "success");
          }
          this.$emit("fetch-data");
          this.close();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
